﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PRO.Controls
{
    [DefaultProperty("PageIndex"), DefaultEvent("PageIndexChanging")]
    public class Pager : WebControl, IPostBackEventHandler
    {
        #region 属性
        //总记录数
        public int RecordCount
        {
            get
            {
                if (ViewState["RecordCount"] != null)
                    return Convert.ToInt32(ViewState["RecordCount"]);
                return 0;
            }
            set
            {
                ViewState["RecordCount"] = value;
            }
        }
        //当前分页索引
        public int PageIndex
        {
            get
            {
                if (ViewState["PageIndex"] != null)
                    return Convert.ToInt32(ViewState["PageIndex"]);
                return 1;
            }
            set
            {
                ViewState["PageIndex"] = value;
            }
        }
        //分页大小
        public int PageSize
        {
            get
            {
                if (ViewState["PageSize"] != null)
                    return Convert.ToInt32(ViewState["PageSize"]);
                return 10;
            }
            set
            {
                ViewState["PageSize"] = value;
            }
        }
        public string PagerCSS
        {
            get
            {
                if (ViewState["PageCssClass"] != null)
                    return ViewState["PageCssClass"].ToString();
                return "";
            }
            set
            {
                ViewState["PageCssClass"] = value;
            }
        }
        //分页样式
        public PagerStyleItems PagerStyle
        {
            get
            {
                if (ViewState["PagerStyle"] == null || ViewState["PagerStyle"].ToString() == "")
                    return PagerStyleItems.Button;
                return (PagerStyleItems)ViewState["PagerStyle"];
            }
            set
            {
                ViewState["PagerStyle"] = value;
            }
        }
        //分页控件的语言
        public PagerLanguageItems PagerLanguage
        {
            get
            {
                if (ViewState["PagerLanguage"] == null || ViewState["PagerLanguage"].ToString() == "")
                    return PagerLanguageItems.EN;
                return (PagerLanguageItems)ViewState["PagerLanguage"];
            }
            set
            {
                ViewState["PagerLanguage"] = value;
            }
        }
        public enum PagerStyleItems
        {
            Button = 0,
            Number = 1,
            Symbol = 2
        }
        public enum PagerLanguageItems
        {
            EN = 0,
            CH = 1
        }
        /// <summary>
        /// 页码式翻页显示多少页数，默认11
        /// </summary>
        public int NumberCounts
        {
            get
            {
                if (ViewState["NumberCounts"] == null || ViewState["NumberCounts"].ToString() == "")
                    return 11;
                return Convert.ToInt32(ViewState["NumberCounts"]);
            }
            set
            {
                ViewState["NumberCounts"] = value;
            }
        }
        #endregion
        #region 事件回传
        static object _PageIndexChanging = new object();

        public event EventHandler<NumericaArgs> PageIndexChanging
        {
            add
            {
                Events.AddHandler(_PageIndexChanging, value);
            }
            remove
            {
                Events.RemoveHandler(_PageIndexChanging, value);
            }
        }

        public void RaisePostBackEvent(string eventArgument)
        {
            //控件自身的事件处理逻辑
            NumericaArgs args = new NumericaArgs(eventArgument);
            //触发用户注册的事件处理逻辑
            if (Events[_PageIndexChanging] != null)
            {
                (Events[_PageIndexChanging] as EventHandler<NumericaArgs>)(null, args);
            }
        }
        public class NumericaArgs : EventArgs
        {
            private int _newPageIndex;

            public int NewPageIndex
            {
                get
                {
                    return _newPageIndex;
                }
                set
                {
                    _newPageIndex = value;
                }
            }
            public NumericaArgs(string args)
            {
                _newPageIndex = Convert.ToInt32(args);
            }
        }
        #endregion
        #region 生成分页控件样式
        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            if (this.RecordCount <= 0)
                return;
            writer.AddAttribute(HtmlTextWriterAttribute.Id, "Pager");
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "Pager" + ((PagerCSS != "") ? " " + PagerCSS : ""));
            writer.RenderBeginTag(HtmlTextWriterTag.Div);//1
            writer.RenderBeginTag(HtmlTextWriterTag.Div);//1
        }
        protected override void RenderContents(HtmlTextWriter writer)
        {
            if (this.RecordCount <= 0)
                return;
            int pageCount = 0;
            if ((RecordCount % PageSize) > 0)
                pageCount = RecordCount / PageSize + 1;
            else
                pageCount = RecordCount / PageSize;
            switch (PagerStyle)
            {
                case PagerStyleItems.Button:
                case PagerStyleItems.Symbol:
                    writer.RenderBeginTag(HtmlTextWriterTag.Label);
                    switch (PagerLanguage)
                    {
                        case PagerLanguageItems.CH:
                            writer.Write(string.Format("第{0}/{1}页&nbsp;&nbsp;共{2}条", PageIndex, pageCount, RecordCount));
                            break;
                        case PagerLanguageItems.EN:
                            writer.Write(string.Format("{0}/{1} Page&nbsp;&nbsp;{2}", PageIndex, pageCount, (RecordCount > 1) ? RecordCount + " Results" : RecordCount + " Result"));
                            break;
                    }
                    writer.RenderEndTag();

                    string first_Ref = Page.ClientScript.GetPostBackClientHyperlink(this, "1");
                    //设置第一页按钮可用状态
                    if (PageIndex <= 1)
                        writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
                    else
                        writer.AddAttribute(HtmlTextWriterAttribute.Href, first_Ref);
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "first");
                    writer.RenderBeginTag(HtmlTextWriterTag.A);

                    switch (PagerStyle)
                    {
                        case PagerStyleItems.Symbol:
                            writer.Write("<<");
                            break;
                        default:
                            switch (PagerLanguage)
                            {
                                case PagerLanguageItems.CH:
                                    writer.Write("第一页");
                                    break;
                                case PagerLanguageItems.EN:
                                    writer.Write("First");
                                    break;
                            }
                            break;
                    }
                    writer.RenderEndTag();
                    //上一页
                    string prev_Ref = Page.ClientScript.GetPostBackClientHyperlink(this, (PageIndex - 1).ToString());//向上加一页
                    if (PageIndex <= 1)
                        writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
                    else
                        writer.AddAttribute(HtmlTextWriterAttribute.Href, prev_Ref);
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "pre");
                    writer.RenderBeginTag(HtmlTextWriterTag.A);

                    switch (PagerStyle)
                    {
                        case PagerStyleItems.Symbol:
                            writer.Write("<");
                            break;
                        default:
                            switch (PagerLanguage)
                            {
                                case PagerLanguageItems.CH:
                                    writer.Write("上一页");
                                    break;
                                case PagerLanguageItems.EN:
                                    writer.Write("Pre");
                                    break;
                            }
                            break;
                    }
                    writer.RenderEndTag();
                    //下一页
                    string next_Ref = Page.ClientScript.GetPostBackClientHyperlink(this, (PageIndex + 1).ToString());//向上加一页
                    if (PageIndex >= pageCount)
                        writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
                    else
                        writer.AddAttribute(HtmlTextWriterAttribute.Href, next_Ref);
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "next");
                    writer.RenderBeginTag(HtmlTextWriterTag.A);

                    switch (PagerStyle)
                    {
                        case PagerStyleItems.Symbol:
                            writer.Write(">");
                            break;
                        default:
                            switch (PagerLanguage)
                            {
                                case PagerLanguageItems.CH:
                                    writer.Write("下一页");
                                    break;
                                case PagerLanguageItems.EN:
                                    writer.Write("Next");
                                    break;
                            }
                            break;
                    }
                    writer.RenderEndTag();
                    //最后一页
                    string last_Ref = Page.ClientScript.GetPostBackClientHyperlink(this, pageCount.ToString());//向上加一页
                    if (PageIndex >= pageCount)
                        writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
                    else
                        writer.AddAttribute(HtmlTextWriterAttribute.Href, last_Ref);
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "last");
                    writer.RenderBeginTag(HtmlTextWriterTag.A);

                    switch (PagerStyle)
                    {
                        case PagerStyleItems.Symbol:
                            writer.Write(">>");
                            break;
                        default:
                            switch (PagerLanguage)
                            {
                                case PagerLanguageItems.CH:
                                    writer.Write("最后一页");
                                    break;
                                case PagerLanguageItems.EN:
                                    writer.Write("Last");
                                    break;
                            }
                            break;
                    }
                    writer.RenderEndTag();
                    break;
                case PagerStyleItems.Number:
                    int left_counts = NumberCounts / 2 + 1;// 当前页的左边显示条数，比如：共11条的话，那么第6条为最中间的那条；如果一共是10条的话，那么也是第6条
                    int right_counts = NumberCounts - left_counts;
                    left_counts--;
                    left_counts--;//因为第一页和最后一页都必须显示，不用计算，减1
                    right_counts--;//因为第一页和最后一页都必须显示，不用计算，减1

                    //上一页
                    string num_prev_Ref = Page.ClientScript.GetPostBackClientHyperlink(this, (PageIndex - 1).ToString());//向上加一页
                    if (PageIndex <= 1 || pageCount <= 1)
                        writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
                    else
                        writer.AddAttribute(HtmlTextWriterAttribute.Href, num_prev_Ref);
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "pre");
                    writer.RenderBeginTag(HtmlTextWriterTag.A);
                    switch (PagerLanguage)
                    {
                        case PagerLanguageItems.CH:
                            writer.Write("上一页");
                            break;
                        case PagerLanguageItems.EN:
                            writer.Write("Pre");
                            break;
                    }
                    writer.RenderEndTag();
                    //输出数字页码
                    if (pageCount <= NumberCounts)
                    {
                        for (int i = 1; i <= pageCount; i++)
                        {
                            string num_Ref = Page.ClientScript.GetPostBackClientHyperlink(this, i.ToString());
                            if (PageIndex == i)
                            {
                                writer.AddAttribute(HtmlTextWriterAttribute.Class, "thisPage");
                                writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
                            }
                            else
                                writer.AddAttribute(HtmlTextWriterAttribute.Href, num_Ref);
                            writer.RenderBeginTag(HtmlTextWriterTag.A);
                            writer.Write(i);
                            writer.RenderEndTag();
                        }
                    }
                    else
                    {
                        string Ref1 = Page.ClientScript.GetPostBackClientHyperlink(this, "1");
                        if (PageIndex <= 1)
                        {
                            writer.AddAttribute(HtmlTextWriterAttribute.Class, "thisPage");
                            writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
                        }
                        else
                        {
                            writer.AddAttribute(HtmlTextWriterAttribute.Href, Ref1);
                        }
                        writer.RenderBeginTag(HtmlTextWriterTag.A);
                        writer.Write(1);
                        writer.RenderEndTag();
                        int start = PageIndex - left_counts;
                        start = (start <= 1) ? 2 : start;
                        int end = (PageIndex <= left_counts) ? left_counts : PageIndex;
                        end = (end >= pageCount) ? pageCount - 1 : end;

                        if ((pageCount - PageIndex) < right_counts + 1)//如果右侧不够数，左侧补位
                            start = start - (right_counts - (pageCount - PageIndex)) - 1;
                        start = (start <= 1) ? 2 : start;
                        if (start >= 3)
                        {
                            writer.RenderBeginTag(HtmlTextWriterTag.Span);
                            writer.Write("…");
                            writer.RenderEndTag();
                        }
                        for (int i = start; i <= end; i++)
                        {
                            string num_Ref = Page.ClientScript.GetPostBackClientHyperlink(this, i.ToString());
                            if (PageIndex == i)
                            {
                                writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
                                writer.AddAttribute(HtmlTextWriterAttribute.Class, "thisPage");
                            }
                            else
                                writer.AddAttribute(HtmlTextWriterAttribute.Href, num_Ref);
                            writer.RenderBeginTag(HtmlTextWriterTag.A);
                            writer.Write(i);
                            writer.RenderEndTag();
                        }
                        int end2 = end + right_counts;
                        end2 = (end2 >= pageCount) ? pageCount - 1 : end2;
                        if (PageIndex < left_counts + 2)
                            end2 = NumberCounts - 1;
                        for (int i = end + 1; i <= end2; i++)
                        {
                            string num_Ref = Page.ClientScript.GetPostBackClientHyperlink(this, i.ToString());
                            writer.AddAttribute(HtmlTextWriterAttribute.Href, num_Ref);
                            writer.RenderBeginTag(HtmlTextWriterTag.A);
                            writer.Write(i);
                            writer.RenderEndTag();
                        }
                        if (pageCount - end2 > 1)
                        {
                            writer.RenderBeginTag(HtmlTextWriterTag.Span);
                            writer.Write("…");
                            writer.RenderEndTag();
                        }
                        string pageCount_Ref = Page.ClientScript.GetPostBackClientHyperlink(this, pageCount.ToString());
                        if (PageIndex >= pageCount)
                        {
                            writer.AddAttribute(HtmlTextWriterAttribute.Class, "thisPage");
                            writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
                        }
                        else
                        {
                            writer.AddAttribute(HtmlTextWriterAttribute.Href, pageCount_Ref);
                        }
                        writer.RenderBeginTag(HtmlTextWriterTag.A);
                        writer.Write(pageCount);
                        writer.RenderEndTag();
                    }
                    //下一页
                    string num_next_Ref = Page.ClientScript.GetPostBackClientHyperlink(this, (PageIndex + 1).ToString());//向上加一页
                    if (PageIndex >= pageCount || pageCount <= 1)
                        writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
                    else
                        writer.AddAttribute(HtmlTextWriterAttribute.Href, num_next_Ref);
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "next");
                    writer.RenderBeginTag(HtmlTextWriterTag.A);
                    switch (PagerLanguage)
                    {
                        case PagerLanguageItems.CH:
                            writer.Write("下一页");
                            break;
                        case PagerLanguageItems.EN:
                            writer.Write("Next");
                            break;
                    }
                    writer.RenderEndTag();
                    break;
            }
        }
        public override void RenderEndTag(HtmlTextWriter writer)
        {
            if (this.RecordCount <= 0)
                return;
            writer.RenderEndTag();//1
            writer.RenderEndTag();//1
        }
        #endregion
    }
}
